import hashlib
import rsa
import base64
from Crypto.Hash import SHA
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_v1_5  # 用于加密/ 解密
from Crypto.Signature import PKCS1_v1_5 as Signature_pkcs1_v1_5


def sha256hex(data):
    sha256 = hashlib.sha256()
    sha256.update(data.encode())
    res = sha256.hexdigest()
    return res


def hashlib_md5(parm):
    m2 = hashlib.md5()
    m2.update(parm.encode('utf-8'))
    return m2.hexdigest()


def md5(parm):
    hash = hashlib.sha1()
    hash.update(parm.encode('utf-8'))
    signature = hash.hexdigest()
    return signature


def get_token(md5str):
    m1 = hashlib.md5()
    m1.update(md5str.encode("utf-8"))
    token = m1.hexdigest()
    token = token.upper()
    return token


def create_keys():  # 生成公钥和私钥
    (pubkey, privkey) = rsa.newkeys(1024)
    pub = pubkey.save_pkcs1()
    with open('tl_Oauth_public.pem', 'wb+')as f:
        f.write(pub)

    pri = privkey.save_pkcs1()
    with open('tl_Oauth_private.pem', 'wb+')as f:
        f.write(pri)


# 私钥签名
def to_sign_with_private_key(content, private_key):
    """
    签名
    :param content:
    :return:
    """
    privatekey = rsa.PrivateKey.load_pkcs1(private_key)
    signature = rsa.sign(content.encode('utf-8'), privatekey, 'SHA-1')
    sign = base64.b64encode(signature).decode('utf-8')
    return sign


# 公钥加密
def encrypt_with_rsa(content, public_key):
    pub = RSA.import_key(public_key)
    cipher = PKCS1_v1_5.new(pub)
    encrypt_bytes = cipher.encrypt(content.encode(encoding='utf-8'))
    return encrypt_bytes


# 私钥解密
def decrypt_with_rsa(encrypt_message, private_key):  # 解密
    private_key = rsa.PrivateKey.load_pkcs1(private_key)
    lase_text = rsa.decrypt(encrypt_message, private_key).decode("utf-8")  # 注意，这里如果结果是bytes类型，就需要进行decode()转化为str
    return lase_text


# 公钥验签
def to_verify_with_public_key(signature, plain_text, public_key):
    rsakey = RSA.importKey(public_key)
    verifier = Signature_pkcs1_v1_5.new(rsakey)
    digest = SHA.new()
    digest.update(plain_text.encode())
    is_verify = verifier.verify(digest, base64.b64decode(signature))
    print(is_verify)
    return bool(is_verify)


if __name__ == '__main__':
    pass
    # create_keys()
    public_key = """-----BEGIN RSA PUBLIC KEY-----
MIIBCgKCAQEAsswloHwspPIU+tKaoCUlhWcZr75+q0XoKrbBvU8cGv7iIY9QSSWW
wZmIisNeglzPEMY5B+rz0nApLJpwNCB99DNXgo5J6BZ/vVVjWAs1bXr+DeIK23Fs
d7fRwNvL9vKUb8EfT7/LcRAdMn/s+20+yX2F5ivmfxEnSDzAJ93ij5T0i1QRYNUX
x+dBXSFkKxcvMv1/GIK4iHcOjT8sa2GtcpqiLUHZpwwYOHQNARpwVR5poqsAw6g+
RLT4vRe2t4wZYZFm7VX0gchKcl0K6H6VLrfrzr3rY/MBnIGvRoWgIgDlFKhSa0Q9
ZP8+8IXW5uX+zN4dyJPKpVsmdlSazPPL5QIDAQAB
-----END RSA PUBLIC KEY-----"""
    private_key = """-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEAsswloHwspPIU+tKaoCUlhWcZr75+q0XoKrbBvU8cGv7iIY9Q
SSWWwZmIisNeglzPEMY5B+rz0nApLJpwNCB99DNXgo5J6BZ/vVVjWAs1bXr+DeIK
23Fsd7fRwNvL9vKUb8EfT7/LcRAdMn/s+20+yX2F5ivmfxEnSDzAJ93ij5T0i1QR
YNUXx+dBXSFkKxcvMv1/GIK4iHcOjT8sa2GtcpqiLUHZpwwYOHQNARpwVR5poqsA
w6g+RLT4vRe2t4wZYZFm7VX0gchKcl0K6H6VLrfrzr3rY/MBnIGvRoWgIgDlFKhS
a0Q9ZP8+8IXW5uX+zN4dyJPKpVsmdlSazPPL5QIDAQABAoIBABTflmj09jVPebmK
iri1fXrkfXAx+hG+uIr8CQQzzyErrnSiiYnwlHgHl5QzmL35XcckjRXqjQp9Q6FM
alr7jBjNQaMhP2mT7tfIty9ZJ6SfXjd6sy/JPxN+50LvphEvYrrlPeWOi46ohfAW
VTFJILJfMBmd+GvPMdnSFdDRq5h9ZlNxC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Y-----"""
    content = "李超123"
    result = encrypt_with_rsa(content, public_key)

    lase_text = decrypt_with_rsa(result, private_key)
    print(lase_text)
